home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / Syn Text Editor 2.1.0.46 / synsetup-2.1.0.46.exe / {app} / scripts / regexpfind.vbs < prev    next >
Text File  |  2003-08-13  |  9KB  |  313 lines

  1. ' Caption: Find/Replace RegExpr...|
  2. ' Hint: Find and Replace with Regular Expression|
  3. ' Icon: regexpfind.ico|
  4. '
  5. '  syn
  6. '  Copyright (C) 2000-2003, Ascher Stefan. All rights reserved.
  7. '  stievie@utanet.at, http://web.utanet.at/ascherst/
  8. '
  9. '  The contents of this file are subject to the Mozilla Public License
  10. '  Version 1.1 (the "License"); you may not use this file except in compliance
  11. '  with the License. You may obtain a copy of the License at
  12. '  http://www.mozilla.org/MPL/
  13. '
  14. '  Software distributed under the License is distributed on an "AS IS" basis,
  15. '  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  16. '  the specific language governing rights and limitations under the License.
  17. '
  18. '  The Original Code is regexpfind.vbs, released Sun, 26 May 2002 10:55:39 UTC.
  19. '
  20. '  The Initial Developer of the Original Code is Ascher Stefan.
  21. '  Portions created by Ascher Stefan are Copyright (C) 2000-2003 Ascher Stefan.
  22. '  All Rights Reserved.
  23. '
  24. '  Contributor(s): .
  25. '
  26. '  Alternatively, the contents of this file may be used under the terms of the
  27. '  GNU General Public License Version 2 or later (the "GPL"), in which case
  28. '  the provisions of the GPL are applicable instead of those above.
  29. '  If you wish to allow use of your version of this file only under the terms
  30. '  of the GPL and not to allow others to use your version of this file
  31. '  under the MPL, indicate your decision by deleting the provisions above and
  32. '  replace them with the notice and other provisions required by the GPL.
  33. '  If you do not delete the provisions above, a recipient may use your version
  34. '  of this file under either the MPL or the GPL.
  35. '
  36. '  You may retrieve the latest version of this file at the syn home page,
  37. '  located at http://syn.sourceforge.net/
  38. '
  39. ' $Id: regexpfind.vbs,v 1.4.2.5 2003/08/13 00:38:45 neum Exp $
  40.  
  41.  
  42. ' Find and replace Text in the Active Document using Regular Expressions
  43.  
  44. option explicit
  45.  
  46. dim FindDialog
  47. dim FindWhatEdit
  48. dim ReplaceWithEdit
  49. dim FindButton, ReplaceButton
  50. dim CancelButton
  51. dim chkIgnoreCase
  52. dim rgScope, rgDirection
  53. dim RegExpr
  54. dim MIndex
  55. dim RMatches
  56. dim SelBegin
  57. const RegKey = "HKCU\Software\Ascher\syn\Macros"
  58.  
  59. '#include <cmnfunc.vbs>
  60. '#include <consts.vbs>
  61.  
  62. sub FindEditChange(Sender)
  63.   FindButton.Enabled = Len(FindWhatEdit.Text) > 0
  64.   ReplaceButton.Enabled = Len(FindWhatEdit.Text) > 0
  65. end sub
  66.  
  67. sub SelectMatch(start, length)
  68.   start = start + SelBegin
  69.   with ActiveDocument
  70.     .SelStart = start
  71.     .SelEnd = start + length
  72.   end with
  73. end sub
  74.  
  75. sub Match()
  76.   dim InString
  77.   AddItem(FindWhatEdit)
  78.   RegExpr.Pattern = FindWhatEdit.Text
  79.   RegExpr.IgnoreCase = chkIgnoreCase.Checked
  80.   RegExpr.Global = true
  81.   if rgScope.ItemIndex = 0 then
  82.     InString = ActiveDocument.Text
  83.     SelBegin = 0
  84.   else
  85.     InString = ActiveDocument.SelText
  86.     SelBegin = Activedocument.SelStart - 1
  87.   end if
  88.   set RMatches = RegExpr.Execute(InString)
  89. end sub
  90.  
  91. sub FindClick(Sender)
  92.   dim RetStr
  93.   dim pos1, pos2
  94.   if RegExpr.Pattern <> FindWhatEdit.Text then
  95.     Match
  96.     if rgDirection.ItemIndex = 0 then
  97.       MIndex = 0
  98.     else
  99.       MIndex = RMatches.Count - 1
  100.     end if
  101.   else
  102.     if rgDirection.ItemIndex = 0 then
  103.       MIndex = MIndex + 1
  104.     else
  105.       MIndex = MIndex - 1
  106.     end if
  107.   end if
  108.   
  109.   if RMatches.Count > 0 then
  110.     if rgDirection.ItemIndex = 0 then
  111.       if MIndex < RMatches.Count then
  112.         pos1 = RMatches(MIndex).FirstIndex
  113.         pos2 = Len(RMatches(MIndex).Value)
  114.         SelectMatch pos1, pos2
  115.       else
  116.         MsgBox "No more Matches found.", vbInformation
  117.       end if
  118.     else
  119.       if MIndex => 0 then               ' 0-based
  120.         pos1 = RMatches(MIndex).FirstIndex
  121.         pos2 = Len(RMatches(MIndex).Value)
  122.         SelectMatch pos1, pos2
  123.       else
  124.         MsgBox "No more Matches found.", vbInformation
  125.       end if
  126.     end if
  127.   else
  128.     MsgBox "No Matches found.", vbInformation
  129.   end if
  130. end sub
  131.  
  132. sub ReplaceClick(Sender)
  133.   AddItem(ReplaceWithEdit)
  134.   if RegExpr.Test(ActiveDocument.SelText) then
  135.     ActiveDocument.SelText = ReplaceWithEdit.Text
  136.     Match
  137.     MIndex = MIndex - 1
  138.   end if
  139.   FindClick(Sender)
  140. end sub
  141.  
  142. sub AddItem(Combo)
  143.   dim tmp
  144.   dim ix
  145.   const MaxHis = 10  ' Change this value to have more or less Items in the ComboBox List
  146.   if Combo.Text = "" then exit sub
  147.  
  148.   tmp = Combo.Text
  149.   ix = Combo.Items.IndexOf(Combo.Text)
  150.   if ix > -1 then
  151.     Combo.Items.Move ix, 0
  152.   else
  153.     Combo.Items.Insert 0, Combo.Text
  154.   end if
  155.   if Combo.Items.Count > MaxHis then
  156.     Combo.Items.Delete MaxHis - 1
  157.   end if
  158.   Combo.Text = tmp
  159. end sub
  160.  
  161. sub FormShow(Sender)
  162.   ' Load Settings
  163.   FindWhatEdit.Items.Text = RegGetSettings(AddBackslash(RegKey) & "re_searchhis", "")
  164.   ReplaceWithEdit.Items.Text = RegGetSettings(AddBackslash(RegKey) & "re_replacehis", "")
  165.   chkIgnoreCase.Checked = RegGetSettings(AddBackslash(RegKey) & "re_ignorecase", false)
  166.   rgScope.ItemIndex = RegGetSettings(AddBackslash(RegKey) & "re_scope", 0)
  167.   rgDirection.ItemIndex = RegGetSettings(AddBackslash(RegKey) & "re_dir", 0)
  168.   if ActiveDocument.BlockBeginY = ActiveDocument.BlockEndY then
  169.     FindWhatEdit.Text = ActiveDocument.WordAtCaret
  170.   end if
  171.   FindEditChange null
  172. end sub
  173.  
  174. sub FormDestroy(Sender)
  175.   ' Save Settings
  176.   if FindWhatEdit.Items.Count > 0 then
  177.     RegSetSettings AddBackslash(RegKey) & "re_searchhis", FindWhatEdit.Items.Text
  178.   end if
  179.   if ReplaceWithEdit.Items.Count > 0 then
  180.     RegSetSettings AddBackslash(RegKey) & "re_replacehis", ReplaceWithEdit.Items.Text
  181.   end if
  182.   RegSetSettings AddBackslash(RegKey) & "re_ignorecase", chkIgnoreCase.Checked
  183.   RegSetSettings AddBackslash(RegKey) & "re_scope", rgScope.ItemIndex
  184.   RegSetSettings AddBackslash(RegKey) & "re_dir", rgDirection.ItemIndex
  185. end sub
  186.  
  187. sub Main(dummy)
  188.   dim Label1, Label2
  189.   if Documents.Count = 0 then
  190.     MsgBox "No Document open. Open a Document and test this excellent Script!!!!!", vbCritical
  191.     exit sub
  192.   end if
  193.   
  194.   FindDialog = Create("TForm", Self, "FindForm")
  195.   with FindDialog
  196.     .Caption = "Find/Replace using Regular Expression"
  197.     .Position = "poOwnerFormCenter"
  198.     .BorderStyle = "bsDialog"
  199.     .Width = 370
  200.     .Height = 180
  201.     .OnShow = "FormShow"
  202.     .OnDestroy = "FormDestroy"
  203.   end with
  204.   
  205.   Label1 = Create("TLabel", FindDialog)
  206.   with Label1
  207.     .Parent = FindDialog
  208.     .Caption = "&Find what:"
  209.     .Left = 8
  210.     .Top = 12
  211.     .FocusControl = FindWhatEdit
  212.   end with
  213.   FindWhatEdit = Create("TComboBox", FindDialog)
  214.   with FindWhatEdit
  215.     .Parent = FindDialog
  216.     .Left = 80
  217.     .Top = 8
  218.     .Width = 190
  219.     .Height = 21
  220.     .OnChange = "FindEditChange"
  221.   end with
  222.   Label2 = Create("TLabel", FindDialog)
  223.   with Label2
  224.     .Parent = FindDialog
  225.     .Caption = "&Replace with:"
  226.     .Left = 8
  227.     .Top = 35
  228.     .FocusControl = ReplaceWithEdit
  229.   end with
  230.   ReplaceWithEdit = Create("TComboBox", FindDialog)
  231.   with ReplaceWithEdit
  232.     .Parent = FindDialog
  233.     .Left = 80
  234.     .Top = 33
  235.     .Width = 190
  236.     .Height = 21
  237.   end with
  238.   
  239.   FindButton = Create("TButton", FindDialog)
  240.   with FindButton
  241.     .Parent = FindDialog
  242.     .Caption = "F&ind"
  243.     .Default = true
  244.     .Left = 280
  245.     .Top = 7
  246.     .Width = 75
  247.     .Enabled = false
  248.     .Hint = "Find Match"
  249.     .OnClick = "FindClick"
  250.   end with
  251.   ReplaceButton = Create("TButton", FindDialog)
  252.   with ReplaceButton
  253.     .Parent = FindDialog
  254.     .Caption = "R&eplace"
  255.     .Left = 280
  256.     .Top = 40
  257.     .Width = 75
  258.     .Enabled = false
  259.     .Hint = "Replace Match"
  260.     .OnClick = "ReplaceClick"
  261.   end with
  262.   CancelButton = Create("TButton", FindDialog)
  263.   with CancelButton
  264.     .Parent = FindDialog
  265.     .Caption = "&Close"
  266.     .Cancel = true
  267.     .ModalResult = mrCancel
  268.     .Left = 280
  269.     .Top = 120
  270.     .Width = 75
  271.     .Hint = "Close this Dialog"
  272.   end with
  273.  
  274.   chkIgnoreCase = Create("TCheckBox", FindDialog)
  275.   with chkIgnoreCase
  276.     .Parent = FindDialog
  277.     .Caption = "&Ignore Case"
  278.     .Left = 8
  279.     .Top = 68
  280.   end with
  281.   rgScope = Create("TRadioGroup", FindDialog)
  282.   with rgScope
  283.     .Parent = FindDialog
  284.     .Caption = "Scope"
  285.     .Left = 8
  286.     .Top = 90
  287.     .Height = 55
  288.     .Width = 130
  289.     .Items.Add("&Entire Document")
  290.     .Items.Add("&Selection only")
  291.     .ItemIndex = 0
  292.   end with
  293.   rgDirection = Create("TRadioGroup", FindDialog)
  294.   with rgDirection
  295.     .Parent = FindDialog
  296.     .Caption = "Direction"
  297.     .Left = 150
  298.     .Top = 90
  299.     .Height = 55
  300.     .Width = 120
  301.     .Items.Add("F&orward")
  302.     .Items.Add("&Backward")
  303.     .ItemIndex = 0
  304.   end with
  305.  
  306.   set RegExpr = new RegExp
  307.  
  308.   FindDialog.ShowModal
  309.   set RegExpr = nothing
  310.   FindDialog.Free
  311. end sub
  312.  
  313.